package cn.six1943.deepchat.module.ai.service.provider;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.six1943.deepchat.module.ai.controller.admin.provider.vo.*;
import cn.six1943.deepchat.module.ai.dal.dataobject.provider.ProviderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.six1943.deepchat.module.ai.dal.mysql.provider.ProviderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.six1943.deepchat.module.ai.enums.ErrorCodeConstants.*;

/**
 * AI提供商 Service 实现类
 *
 * @author at1943
 */
@Service
@Validated
public class ProviderServiceImpl implements ProviderService {

    @Resource
    private ProviderMapper providerMapper;

    @Override
    public Long createProvider(ProviderSaveReqVO createReqVO) {
        // 插入
        ProviderDO provider = BeanUtils.toBean(createReqVO, ProviderDO.class);
        providerMapper.insert(provider);
        // 返回
        return provider.getId();
    }

    @Override
    public void updateProvider(ProviderSaveReqVO updateReqVO) {
        // 校验存在
        validateProviderExists(updateReqVO.getId());
        // 更新
        ProviderDO updateObj = BeanUtils.toBean(updateReqVO, ProviderDO.class);
        providerMapper.updateById(updateObj);
    }

    @Override
    public void deleteProvider(Long id) {
        // 校验存在
        validateProviderExists(id);
        // 删除
        providerMapper.deleteById(id);
    }

    private void validateProviderExists(Long id) {
        if (providerMapper.selectById(id) == null) {
            throw exception(PROVIDER_NOT_EXISTS);
        }
    }

    @Override
    public ProviderDO getProvider(Long id) {
        return providerMapper.selectById(id);
    }

    @Override
    public PageResult<ProviderDO> getProviderPage(ProviderPageReqVO pageReqVO) {
        return providerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProviderRespVO> getProviderList(Long userTenantId) {
        return providerMapper.selectAppProvider(userTenantId);
    }

}